package com.boot.message.netty.client;

import com.boot.message.mail.Email;
import com.boot.message.netty.client.handler.AbstractClientChannelInitializer;
import com.boot.message.netty.client.handler.BaseClientHandler;
import com.boot.message.netty.client.handler.impl.JSONClientHandler;
import com.boot.message.netty.client.handler.impl.JSONClientInitializer;
import com.boot.message.netty.client.handler.impl.StringClientHandler;
import com.boot.message.netty.client.handler.impl.StringClientInitializer;
import com.boot.message.netty.client.sender.NettyClientSender;
import com.boot.message.netty.client.sender.impl.JSONSender;
import com.boot.message.netty.client.sender.impl.StringSender;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.socket.SocketChannel;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description netty客户端处理器配置（默认实现）
 * @date 2020/1/4 17:43
 **/
//@DependsOn("JSONUtil")
@Configuration
public class NettyClientConfig {

    @Bean
    @ConditionalOnMissingBean(BaseClientHandler.class)
    BaseClientHandler stringClientHandler() {
        return new StringClientHandler();
    }

    @Bean
    @ConditionalOnMissingBean(AbstractClientChannelInitializer.class)
    AbstractClientChannelInitializer<SocketChannel> stringClientChannelInitializer(BaseClientHandler baseClientHandler) {
        return new StringClientInitializer(baseClientHandler);
    }

    @Bean
    @ConditionalOnMissingBean(NettyClientSender.class)
    NettyClientSender stringSender(NettyClientProperties nettyClientProperties, AbstractClientChannelInitializer<SocketChannel> clientChannelInitializer) {
        return new StringSender(nettyClientProperties, clientChannelInitializer);
    }

    @Primary
    @Bean
    NettyClientSender jsonSender(NettyClientProperties nettyClientProperties, ObjectMapper om) {
        return new JSONSender(nettyClientProperties, new JSONClientInitializer(new JSONClientHandler(om, Email.class)), om);
    }

}
